import upload from '@/views/modules/oss/oss-upload'
import fileInfo from '@/views/modules/oss/oss-file-info'
import ossShareCreate from '@/views/modules/oss/oss-share-create'
import Cookies from 'js-cookie'
export default {
  data () {
    return {
      downloadUrl: '',
      /* 上传，弹窗visible状态 */
      uploadVisible: false,
      /* 文件属性，弹窗visible状态 */
      fileInfoVisible: false,
      /* 文件分享，弹窗visible状态 */
      shareFileVisible: false,
      // 文件列表数据
      fileDataList: []
    }
  },
  components: {
    upload,
    fileInfo,
    ossShareCreate
  },
  created () {
    this.downloadUrl = `${window.SITE_CONFIG.apiURL}/sys/oss/download?token=${Cookies.get(window.SITE_CONFIG.$config.tokenKeySign + 'token')}`
  },
  methods: {
    // 获取当前工作空间路径
    getWorkspacePath () {
      const pathList = [...this.$refs.ossList.pathList]
      pathList.splice(0, 1)
      let result = ''
      if (pathList.length >= 1) {
        result += pathList.join('\\') + '\\'
      }
      if (!result) {
        result = '\\'
      }
      return result
    },
    // 选择文件
    uploadChooseFile (e) {
      this.ossUploadHandle({
        // 1 进行中 2 取消 3 暂停 4 完成 5失败
        status: 1,
        // 是否初次载入
        isNew: true,
        // 已经成功的片数
        successCounts: 0,
        // 上一次计算时间
        lastTime: new Date().getTime(),
        // 上一次计算的文件大小
        lastSize: 0,
        // 区间内已传输字节大小
        intervalSize: 0,
        // 速度
        speed: '',
        // 剩余时间
        timeRemaining: '',
        // 上传进度
        process: 0,
        nodePath: '',
        name: e.file.name,
        size: e.file.size,
        file: e.file
      })
    },
    // 参数配置
    ossUploadHandle (fileData) {
      this.uploadVisible = true
      this.$nextTick(() => {
        this.$refs.upload.visible = true
        this.$refs.upload.pushFile(fileData)
      })
    },
    getFileDataList (path) {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0)'
      })
      this.fileDataList = []
      this.$http.get('/sys/oss/list', {
        params: {
          path: path
        }
      }).then(({ data: res }) => {
        loading.close()
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.fileDataList = res.data
      }).catch(() => {
        loading.close()
      })
    },
    // 右击菜单条目点击
    rightMenuItemClick (type) {
      if (type === 'separate') { // 分隔符点击拦截
        return false
      }
      if (type === 'openDir' && this.rightClickData.name) { // 打开文件夹
        this.$refs.ossList.pathList.push(this.rightClickData.name)
        this.initFileDataList()
        // 隐藏菜单
        this.hideRightMenu()
      } else if (type === 'reload') { // 刷新
        // 隐藏菜单
        this.hideRightMenu()
        this.initFileDataList()
      } else if (type === 'createDir') { // 新建文件夹
        // 隐藏菜单
        this.hideRightMenu()
        this.$prompt('请输入文件夹名称', '新建文件夹', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          closeOnClickModal: false,
          // eslint-disable-next-line
          inputPattern: /^[^\/:\*\?""<>|]{1,120}$/,
          inputErrorMessage: '非法文件夹名称'
        }).then(({ value }) => {
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0)'
          })
          this.$http.post('/sys/oss/createDir', { path: this.getWorkspacePath() + value }).then(({ data: res }) => {
            loading.close()
            if (res.code !== 0) {
              return this.$message.error(res.msg)
            }
            this.initFileDataList()
            // return this.$message.success('创建成功')
          }).catch(() => {
            loading.close()
          })
        }).catch(() => {})
      } else if (type === 'delete') { // 删除文件
        // 隐藏菜单
        this.hideRightMenu()
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.5)'
          })
          this.$http.post('/sys/oss/deleteFile', { path: this.getWorkspacePath() + this.rightClickData.name }).then(({ data: res }) => {
            loading.close()
            if (res.code !== 0) {
              return this.$message.error(res.msg)
            }
            this.initFileDataList()
            return this.$message.success('删除成功')
          }).catch(() => {
            loading.close()
          })
        }).catch(() => {})
      } else if (type === 'reName') { // 重命名
        // 隐藏菜单
        this.hideRightMenu()
        this.$prompt('请输入文件名称', '重命名', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          closeOnClickModal: false,
          // 初始值
          inputValue: this.rightClickData.name || '',
          // eslint-disable-next-line
          inputPattern: /^[^\/:\*\?""<>|]{1,120}$/,
          inputErrorMessage: '非法文件名称'
        }).then(({ value }) => {
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0)'
          })
          this.$http.post('/sys/oss/reName', { path: this.getWorkspacePath() + this.rightClickData.name, fileName: value }).then(({ data: res }) => {
            loading.close()
            if (res.code !== 0) {
              return this.$message.error(res.msg)
            }
            this.initFileDataList()
          }).catch(() => {
            loading.close()
          })
        }).catch(() => {})
      } else if (type === 'info') { // 属性
        // 隐藏菜单
        this.hideRightMenu()
        this.fileInfoVisible = true
        this.$nextTick(() => {
          this.$refs.fileInfo.dataForm.path = this.getWorkspacePath() + this.rightClickData.name
          this.$refs.fileInfo.dataForm.filename = this.rightClickData.name
          this.$refs.fileInfo.dataForm.isDir = this.rightClickData.isDir
          this.$refs.fileInfo.dataForm.suffix = this.rightClickData.suffix
          this.$refs.fileInfo.init()
        })
      } else if (type === 'downloadFile') { // 下载文件
        const url = this.downloadUrl + '&path=' + encodeURI(this.getWorkspacePath() + this.rightClickData.name)
        location.href = url
        // 隐藏菜单
        this.hideRightMenu()
      } else if (type === 'shareFile') { // 分享文件
        // 隐藏菜单
        this.hideRightMenu()
        this.shareFileVisible = true
        this.$nextTick(() => {
          this.$refs.ossShareCreate.dataForm.path = this.getWorkspacePath() + this.rightClickData.name
          this.$refs.ossShareCreate.init()
        })
      }
    },
    hideRightMenu () {
      this.$refs.ctxshow.hideMenu() // 隐藏菜单
    }
  }
}
